<template>
  <d2-container>
    <!-- <loading :active="isLoading" :is-full-page="false"/> -->
    <!-- header 查询条件 -->
    <template slot="header">
      <el-form :inline="true" :model="listQuery" size="mini" style="margin-bottom: -18px;">
        <el-form-item label="用户名" prop="username">
          <el-input
            @keyup.enter.native="handleFilter"
            style="width: 200px;"
            placeholder="用户名"
            v-model="listQuery.username"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="default" @click="handleFilter" icon="el-icon-search">搜 索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button
            v-if="sys_user_add"
            @click="handleCreate"
            type="primary"
            icon="el-icon-plus"
          >新 增</el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- table表格 -->
    <el-table :key="tableKey" :data="list" highlight-current-row stripe style="width: 100%">
      <el-table-column type="index" label="序号" header-align="center" align="center"></el-table-column>
      <el-table-column align="center" label="用户id" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.userId}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="登录名">
        <template slot-scope="scope">
          <span>
            <img
              v-if="scope.row.avatar"
              class="user-avatar"
              style="width: 20px; height: 20px; border-radius: 50%;"
              :src="scope.row.avatar+'?imageView2/1/w/20/h/20'"
            />
            {{scope.row.username}}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名">
        <template slot-scope="scope">
          <span>{{scope.row.realName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="分机号">
        <template slot-scope="scope">
          <span>{{scope.row.extentionNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="所属部门" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.deptName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色" width="200">
        <template slot-scope="scope">
          <span v-for="role in scope.row.roleList" :key="role.id">{{role.roleName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="级别" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.achievementTypeLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="标签">
        <template slot-scope="scope">
          <span>{{scope.row.label}}</span>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <el-tag>{{scope.row.delFlag | statusFilter}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="sys_user_upd"
            size="mini"
            type="primary"
            @click="handlePwd(scope.row)"
            icon="fa fa-key"
          ></el-button>
          <el-button
            v-if="sys_user_upd"
            size="mini"
            type="primary"
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
          ></el-button>
          <el-button
            v-if="sys_user_del"
            size="mini"
            type="danger"
            @click="deletes(scope.row)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- footer 分页条 -->
    <template slot="footer">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page"
        :page-sizes="[50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin: -10px;"
      ></el-pagination>
    </template>
    <!-- 部门筛选 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogDeptVisible" width="600px">
      <el-tree
        class="filter-tree"
        :data="treeDeptData"
        :default-checked-keys="checkedKeys"
        check-strictly
        node-key="id"
        highlight-current
        ref="deptTree"
        :props="defaultProps"
        @node-click="getNodeData"
        default-expand-all
      ></el-tree>
    </el-dialog>
    <!-- 新增用户弹框 -->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      width="800px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="80px" size="mini">
        <el-row style="width:100%">
          <el-col :span="12">
            <el-form-item label="登录名" prop="username">
              <el-input v-model="form.username" placeholder="请输登录名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名" prop="realName">
              <el-input v-model="form.realName" placeholder="请输用户名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分机号" prop="extentionNumber">
              <el-input v-model="form.extentionNumber" placeholder="请输分机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" placeholder="请输入密码" prop="newpassword1">
              <el-input
                :disabled="dialogStatus == 'update'"
                type="password"
                v-model="form.newpassword1"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="deptName">
              <el-input v-model="form.deptName" placeholder="选择部门" @focus="handleDept()" readonly></el-input>
              <input type="hidden" v-model="form.deptId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色" prop="roleList">
              <el-select
                v-model="form.roleList"
                value-key="roleId"
                placeholder="请选择"
                multiple
                collapse-tags
                style="width:100%;"
              >
                <el-option
                  v-for="item in rolesOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item"
                  :disabled="isDisabled[item.delFlag]"
                >
                  <!-- <span style="float: left">{{ item.roleDesc }}</span> -->
                  <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.roleCode }}</span> -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="验证码登录使用"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="delFlag">
              <el-select
                v-model="form.delFlag"
                placeholder="请选择"
                :disabled="!sys_user_del"
                style="width:100%;"
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item"
                  :label="item | statusFilter"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="级别" prop="achievementType">
              <el-select v-model="form.achievementType" placeholder="请选择" style="width:100%;">
                <el-option
                  v-for="item in achievementTypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
        <!-- <el-row>
          <el-form-item label="标签" prop="label">
            <el-input v-model="form.label" placeholder="多个标签 ',' 隔开"></el-input>
          </el-form-item>
        </el-row>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')" icon="el-icon-close" size="mini">取 消</el-button>
        <el-button
          v-if="dialogStatus=='create'"
          type="primary"
          @click="create('form')"
          icon="el-icon-check"
          size="mini"
        >确 定</el-button>
        <el-button
          v-else
          type="primary"
          @click="update('form')"
          icon="el-icon-check"
          size="mini"
        >修 改</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  resetPwd
} from "@/api/user";
import { remote } from "@/api/dict";
import { deptRoleList, fetchDeptTree } from "@/api/role";
import { mapGetters, mapState } from "vuex";
import ElRadioGroup from "element-ui/packages/radio/src/radio-group";
import ElOption from "element-ui/packages/select/src/option";
// Import component
import Loading from "vue-loading-overlay";
// Import stylesheet
import "vue-loading-overlay/dist/vue-loading.css";
export default {
  components: {
    ElOption,
    ElRadioGroup,
    Loading
  },
  data() {
    return {
      isLoading: false,
      treeDeptData: [],
      checkedKeys: [],
      defaultProps: {
        children: "children",
        label: "name"
      },
      list: null,
      total: null,
      listQuery: {
        page: 1,
        limit: 50
      },
      form: {
        username: undefined,
        realName: undefined,
        extentionNumber: undefined,
        newpassword1: undefined,
        delFlag: undefined,
        deptId: undefined,
        phone: undefined,
        label: undefined,
        achievementType: undefined,
        roleList: []
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入账户",
            trigger: "blur"
          },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur"
          }
        ],
        realName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          },
          {
            min: 2,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur"
          }
        ],
        extentionNumber: [
          {
            message: "请输入分机号",
            trigger: "blur"
          },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur"
          }
        ],
        newpassword1: [
          {
            // required: true,
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur"
          }
        ],
        deptId: [
          {
            required: true,
            message: "请选择部门",
            trigger: "blur"
          }
        ],
        roleList: [
          {
            type: "array",
            required: true,
            message: "请选择角色",
            trigger: "change"
          }
        ],
        phone: [
          {
            required: true,
            message: "手机号",
            trigger: "blur"
          },
          {
            min: 11,
            max: 11,
            message: "长度在11 个字符",
            trigger: "blur"
          }
        ]
      },
      statusOptions: ["0", "1"],
      achievementTypeOptions: [],
      rolesOptions: [],
      dialogFormVisible: false,
      dialogDeptVisible: false,
      userAdd: false,
      userUpd: false,
      userDel: false,
      dialogStatus: "",
      textMap: {
        update: "编辑用户",
        create: "新增用户"
      },
      isDisabled: {
        0: false,
        1: true
      },
      tableKey: 0
    };
  },
  computed: {
    // ...mapGetters(['permissions']),
    ...mapState("d2admin/user", ["info"]),
    ...mapState('d2admin/page', ['opened','current'])
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: "有效",
        1: "无效",
        9: "锁定"
      };
      return statusMap[status];
    }
  },
  created() {
    this.getList();
    this.achievementTypeList();
    this.sys_user_add = this.hasFunctions(["sys_user_add"]);
    this.sys_user_upd = this.hasFunctions(["sys_user_upd"]);
    this.sys_user_del = this.hasFunctions(["sys_user_del"]);
  },
  methods: {
    achievementTypeList() {
      remote("achievementType").then(response => {
        this.achievementTypeOptions = response;
      });
    },
    getList() {
      this.isLoading = true;
      this.listQuery.isAsc = false;
      fetchList(this.listQuery).then(response => {
        this.list = response.records;
        this.total = response.total;
        this.isLoading = false;
      });
    },
    getNodeData(data) {
      this.dialogDeptVisible = false;
      this.form.deptId = data.id;
      this.form.deptName = data.name;
      this.form.roleList = [];
      deptRoleList(data.id).then(response => {
        this.rolesOptions = response;
      });
    },
    handleDept() {
      fetchDeptTree().then(response => {
        this.treeDeptData = response;
        this.dialogDeptVisible = true;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      // deptRoleList(this.info.deptId).then(response => {
      //   this.rolesOptions = response.data
      // })
    },
    handleUpdate(row) {
      getObj(row.userId).then(response => {
        this.form = response;
        this.dialogFormVisible = true;
        this.dialogStatus = "update";
        deptRoleList(response.deptId).then(response => {
          this.rolesOptions = response;
        });
      });
    },
    handlePwd(row) {
      this.$confirm(
        "确认重置密码么?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        let param = {};
        param.username = row.username;
        resetPwd(param).then(response => {
          if (response.result) {
            if (response.status === "SUCCEED") {
              this.$notify({
                title: "成功",
                message: "重置密码成功",
                type: "success"
              });
            }
          }
        });
      });
    },
    create(formName) {
      const set = this.$refs;
      this.form.role = this.role;
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      // this.form.role = this.role
      console.log(this.form);
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          this.form.password = undefined;
          putObj(this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    deletes(row) {
      this.$confirm(
        "此操作将永久删除该用户(用户名:" + row.username + "), 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delObj(row.userId)
          .then(() => {
            this.getList();
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
              duration: 2000
            });
          })
          .cache(() => {
            this.$notify({
              title: "失败",
              message: "删除失败",
              type: "error",
              duration: 2000
            });
          });
      });
    },
    resetTemp() {
      this.form = {
        id: undefined,
        username: "",
        password: "",
        roleList: [],
        delFlag: "",
        deptId: "",
        phone: "",
        achievementType: ""
      };
    }
  }
};
</script>
